/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * Funcionario.java
 *
 * Created on 23/12/2011, 10:57:56
 */
package br.com.bancofloripa.view;

import br.com.bancofloripa.controller.ContaController;
import br.com.bancofloripa.dao.ContaDAO;
import br.com.bancofloripa.dao.ContaOuroDAO;
import br.com.bancofloripa.dao.ContaPoupancaDAO;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.swing.JOptionPane;

/**
 *
 * @author carlos
 */
public class SaqueUi extends javax.swing.JInternalFrame {
    
    private ContaController cc = new ContaController();
    private ContaDAO cd;
    private ContaOuroDAO cod;
    private ContaPoupancaDAO cop;
    
    private String tipo;

    /** Creates new form Funcionario */
    public SaqueUi() {
        initComponents();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        buttonGroup1 = new javax.swing.ButtonGroup();
        jPanel2 = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTable1 = new javax.swing.JTable();
        jRBSimples1 = new javax.swing.JRadioButton();
        jRBOuro1 = new javax.swing.JRadioButton();
        jRBPoup1 = new javax.swing.JRadioButton();
        jToolBar2 = new javax.swing.JToolBar();
        jLabel1 = new javax.swing.JLabel();
        jButtonSaque = new javax.swing.JButton();
        campoValor = new javax.swing.JFormattedTextField();
        jLabel2 = new javax.swing.JLabel();
        jScrollPane3 = new javax.swing.JScrollPane();
        jTextArea1 = new javax.swing.JTextArea();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();

        setMaximizable(true);
        setResizable(true);

        jPanel2.setBorder(javax.swing.BorderFactory.createTitledBorder("Selecionar conta"));

        jTable1.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null},
                {null, null},
                {null, null},
                {null, null}
            },
            new String [] {
                "Conta", "Nome"
            }
        ) {
            boolean[] canEdit = new boolean [] {
                false, false
            };

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        jTable1.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jTable1MouseClicked(evt);
            }
        });
        jScrollPane1.setViewportView(jTable1);

        jRBSimples1.setText("Simples");
        jRBSimples1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jRBSimples1ActionPerformed(evt);
            }
        });

        jRBOuro1.setText("Ouro");
        jRBOuro1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jRBOuro1ActionPerformed(evt);
            }
        });

        jRBPoup1.setText("Poupança");
        jRBPoup1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jRBPoup1ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 259, Short.MAX_VALUE))
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addGap(22, 22, 22)
                        .addComponent(jRBSimples1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jRBOuro1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jRBPoup1)))
                .addContainerGap())
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jRBSimples1)
                    .addComponent(jRBOuro1)
                    .addComponent(jRBPoup1))
                .addGap(15, 15, 15)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 117, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        jToolBar2.setFloatable(false);
        jToolBar2.setRollover(true);

        jLabel1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/br/com/bancofloripa/images/save24.png"))); // NOI18N
        jToolBar2.add(jLabel1);

        jButtonSaque.setText("Saque");
        jButtonSaque.setFocusable(false);
        jButtonSaque.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        jButtonSaque.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        jButtonSaque.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonSaqueActionPerformed(evt);
            }
        });
        jToolBar2.add(jButtonSaque);

        campoValor.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.NumberFormatter(new java.text.DecimalFormat("#0"))));
        campoValor.setHorizontalAlignment(javax.swing.JTextField.RIGHT);

        jLabel2.setText("Valor:");

        jTextArea1.setColumns(20);
        jTextArea1.setEditable(false);
        jTextArea1.setRows(5);
        jScrollPane3.setViewportView(jTextArea1);

        jLabel3.setText("Console:");

        jLabel4.setText(",00");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jToolBar2, javax.swing.GroupLayout.DEFAULT_SIZE, 648, Short.MAX_VALUE)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(jLabel2)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(campoValor, javax.swing.GroupLayout.PREFERRED_SIZE, 111, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jLabel4))
                    .addComponent(jLabel3)
                    .addComponent(jScrollPane3, javax.swing.GroupLayout.DEFAULT_SIZE, 331, Short.MAX_VALUE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jToolBar2, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(17, 17, 17)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel2)
                            .addComponent(campoValor, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel4))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jLabel3)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jScrollPane3)))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jButtonSaqueActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonSaqueActionPerformed
        Pattern pegaJava = Pattern.compile("\\d");  
        Matcher m = pegaJava.matcher(campoValor.getText());  
  
        if (!m.find()) {  
            JOptionPane.showMessageDialog(this, "Deve ser numero");
            return;
        }

        if (tipo !=null ){
            cc.setTipo1(tipo);
            if (this.tipo.equals("simples")) {
                cc.saque(jTextArea1, cc.getCsOrigem(), Integer.parseInt(campoValor.getText()));
            } else if (this.tipo.equals("ouro")) {
                cc.saque(jTextArea1, cc.getCsOrigem(), Integer.parseInt(campoValor.getText()));
            } else if (this.tipo.equals("poup")) {
                cc.saque(jTextArea1, cc.getCsOrigem(), Integer.parseInt(campoValor.getText()));
            }
        } else {
            JOptionPane.showMessageDialog(rootPane, "Selecione uma conta");
        }
        this.campoValor.setText("");
        JOptionPane.showMessageDialog(null, "Deposito efetuado com sucesso");
        dispose();
    }//GEN-LAST:event_jButtonSaqueActionPerformed

    private void jTable1MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jTable1MouseClicked
        cc.getClique(evt, jTable1, this.tipo, "origem");
    }//GEN-LAST:event_jTable1MouseClicked

    private void jRBSimples1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jRBSimples1ActionPerformed
        this.tipo = "simples";
        this.jRBOuro1.setSelected(false);
        this.jRBPoup1.setSelected(false);
        
        cc.atualizaTabela(cc.montandoList(tipo), jTable1);
    }//GEN-LAST:event_jRBSimples1ActionPerformed

    private void jRBOuro1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jRBOuro1ActionPerformed
        this.tipo = "ouro";
        this.jRBSimples1.setSelected(false);
        this.jRBPoup1.setSelected(false);        
        cc.atualizaTabela(cc.montandoList(tipo), jTable1);
    }//GEN-LAST:event_jRBOuro1ActionPerformed

    private void jRBPoup1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jRBPoup1ActionPerformed
        this.tipo = "poup";
        this.jRBOuro1.setSelected(false);
        this.jRBSimples1.setSelected(false);        
        cc.atualizaTabela(cc.montandoList(tipo), jTable1);
    }//GEN-LAST:event_jRBPoup1ActionPerformed
        
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.ButtonGroup buttonGroup1;
    private javax.swing.JFormattedTextField campoValor;
    private javax.swing.JButton jButtonSaque;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JRadioButton jRBOuro1;
    private javax.swing.JRadioButton jRBPoup1;
    private javax.swing.JRadioButton jRBSimples1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane3;
    private javax.swing.JTable jTable1;
    private javax.swing.JTextArea jTextArea1;
    private javax.swing.JToolBar jToolBar2;
    // End of variables declaration//GEN-END:variables
}
